<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Login extends Base{
	public function register(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '请填写手机号']);
		}
		if(!empty(input("param.password"))){
			$data['password']=md5(md5('tmd'.input("param.password")));
		}else{
			return json(['code' => 502, 'msg' => '密码不能为空']);exit();
		}
		if(!empty(input("param.invite_code"))){
			$data['invite_code']=input("param.invite_code");
			$invite_code=Db::name('users')->where(array('invite_code'=>$data['invite_code']))->find();
			if($invite_code){
			}else{
				return json(['code' => 505, 'msg' => '邀请码错误']);
			}
		}
		if(!empty(input("param.openid"))){
			$data['openid']=input("param.openid");
		}
        $user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if (empty($user)) {
			if(isset($invite_code)){
				$data['invite_id']=$invite_code['id'];
			}
			$data['invite_code']=$this->inviteCode();
			$data['time']=time();
			$data['uptime']=time();
			$id=Db::name('users')->insertGetId($data);
			if($id){
				$res['Token']=UserCrypt::do_mencrypt($id);
				return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
			}else{
				return json(['code' => 500, 'msg' => '注册失败']);
			}
			
		}else{
			return json(['code' => 504, 'msg' => '该手机号注册过了']);
		}
	}
  	//登录
	public function login(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '手机号不能为空']);
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 503, 'msg' => '密码不能为空']);exit();
		}
		$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if ($user) {
			if($user['password']==md5(md5('tmd'.$data['password']))){
				if(!empty(input("param.openid"))){
					$data['openid']=input("param.openid");
					Db::name('users')->where(array('phone'=>$data['phone']))->update(['openid'=>$data['openid']]);
				}
				$res['Token']=UserCrypt::do_mencrypt($user['id']);
				return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
			}else{
				return json(['code' => 505, 'msg' => '密码错误']);
			}
		}else{
			return json(['code' => 504, 'msg' => '手机号不存在']);
		}
		
	}
	public function change_password()
	{
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌']);
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 502, 'msg' => '登录密码不能为空']);
		}
		if(!empty(input("param.new_password"))){
			$data['new_password']=input("param.new_password");
		}else{
			return json(['code' => 503, 'msg' => '新密码不能为空']);
		}
		if(!empty(input("param.confirm_password"))){
			$data['confirm_password']=input("param.confirm_password");
		}else{
			return json(['code' => 504, 'msg' => '确认密码不能为空']);
		}
		if($data['new_password']==$data['confirm_password']){
			$uid=UserCrypt::do_mdecrypt($token);
	        $user=Db::name('users')->where(array('id'=>$uid))->find();
	        if ($user) {
				if($user['password']==md5(md5('tmd'.$data['password']))){
					$res=Db::name('users')->where('id',$uid)->update(['password'=>md5(md5('tmd'.$data['new_password']))]);
					if($res){
						return json(['code' => 200, 'msg' => '成功']);
					}else{
						return json(['code' => 508, 'msg' => '失败']);
					}
				}else{
					return json(['code' => 506, 'msg' => '登录密码错误']);
				}
			}else{
				return json(['code' => 507, 'msg' => '失败']);
			}
		}else{
			return json(['code' => 505, 'msg' => '新密码和确认密码不一致']);
		}
        
	}
	private function inviteCode()
	{
		$invite_code=$this->inviteCodeRandom(8);
		$isInviteCode=Db::name('users')->where(array('invite_code'=>$invite_code))->find();
		if($isInviteCode){
			$this->inviteCode();
		}else{
			return $invite_code;
		}
	}
	private function inviteCodeRandom($length = 8) {
	    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	    $str = "";
	    for ($i = 0; $i < $length; $i++) {
	      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
	    }
	    return $str;
	}
}